package com.demo.test.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TransactionTest2Service {



    @Transactional(rollbackFor = Exception.class)
    public void performNestedOperation(String operationType) throws Exception {
        System.out.println("执行嵌套业务操作: " + operationType);

        // 调用另一个带事务的方法
        performOperationWithRollback(operationType);

        System.out.println("嵌套业务操作执行完毕");
    }

    @Transactional(rollbackFor = Exception.class)
    public void performOperationWithRollback(String operationType) throws Exception {
        // 模拟业务操作
        System.out.println("执行业务操作: " + operationType);

        // 模拟可能导致回滚的操作
        if ("rollback".equals(operationType)) {
            throw new RuntimeException("模拟业务异常，触发事务回滚");
        }

        // 正常业务逻辑
        System.out.println("业务操作执行成功");
    }

}